package com.anycl.customer.controller;


import com.anycl.customer.constant.Constants;
import com.anycl.customer.entity.Customer;
import com.anycl.customer.exception.CustomerException;
import com.anycl.customer.service.AdminUserService;
import com.anycl.customer.service.CustomerService;
import com.anycl.customer.vo.AdminUserVo;
import com.anycl.customer.vo.CustomerResponse;
import com.anycl.customer.vo.LoginVo;
import com.anycl.customer.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Slf4j
public class IndexController {

    private final CustomerService customerService;

    private final AdminUserService adminUserService;

    public IndexController(CustomerService customerService, AdminUserService adminUserService) {
        this.customerService = customerService;
        this.adminUserService = adminUserService;
    }

    @GetMapping("/index")
    public String index(ModelMap map) {
        map.addAttribute("name", "客户管理系统");
        map.addAttribute("loginVo", new LoginVo());
        return "index";
    }


    @GetMapping("/list")
    public String list(ModelMap map, HttpSession session) throws CustomerException {
        List<Customer> customerList = customerService.getCustomerList(session);
        map.addAttribute("customerList", customerList);
        return "list";
    }


    @GetMapping("/add")
    public String add(ModelMap map, HttpSession session) throws CustomerException {
        Customer customer = new Customer();
        map.addAttribute("customer", customer);
        return "add";
    }


    @PostMapping("/add")
    public String addCustomer(@ModelAttribute Customer customer,ModelMap map, HttpSession session) throws CustomerException {
        customerService.createCustomer(customer, session);
        map.addAttribute("result", "添加用户信息成功");
        return "result";
    }


    @GetMapping("/register")
    public String register(ModelMap modelMap) throws CustomerException {
        RegisterVo registerVo = new RegisterVo();
        modelMap.addAttribute("registerVo", registerVo);
        return "register";
    }

    @PostMapping("/register")
    public String register(@ModelAttribute RegisterVo registerVo , ModelMap modelMap) throws CustomerException {
        log.info("registerVo  " + registerVo);
        LoginVo loginVo = registerVo;
        AdminUserVo adminUserVo = adminUserService.signIn(loginVo, registerVo.getAuthCode());
        modelMap.addAttribute("result", adminUserVo.getUsername() + "注册成功");
        return "result";
    }

    @PostMapping("/login")
    public String login(@ModelAttribute LoginVo loginVo, ModelMap modelMap,  HttpSession httpSession) throws CustomerException {
        AdminUserVo adminUserVo =  adminUserService.login(loginVo);
        if (adminUserVo.getStatus().equals(Constants.SuccessMsg.SUCCESS_STATUS)) {
            httpSession.setAttribute(Constants.CURRENT_USER, adminUserVo);
        }
        modelMap.addAttribute("result", adminUserVo.getUsername() + "登录成功");
        return "result";
    }


    @GetMapping("/get")
    public String getCustomer(String id, ModelMap modelMap, HttpSession session) throws CustomerException {
        Customer customer = customerService.getCustomer(id, session);
        modelMap.addAttribute("customer", customer);
        return "info";
    }

    @PostMapping("/update")
    public String updateCustomer(@ModelAttribute Customer customer, ModelMap modelMap, HttpSession session) throws CustomerException {
        CustomerResponse customerResponse = customerService.updateCustomer(customer, session);
        modelMap.addAttribute("result", customerResponse.getName() + "的信息更新成功");
        return "result";
    }





}
